import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Data {
	
	private int[] frequency;
	private int[] lastdraw;
	
	public Data(String filename){
		frequency = new int[49];
		lastdraw = new int[49];
		//Int array need to be initialised?
		for(int i = 0;i < frequency.length; i++){
			frequency[i] = 0;
			lastdraw[i] = 0;
		}
		
		copyFileContents(filename);
		//findLeastCommon();
	}
	
	public void copyFileContents(String filename){
		try {
			BufferedReader input = new BufferedReader(new FileReader("lotto.csv"));
			String line = null;
			while((line = input.readLine()) != null){
				//System.out.println(line);
				extractDrawNums(line);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public void customMethod(){
		frequency[45]--;
		frequency[15]--;
		frequency[17]--;
		frequency[9]--;
		frequency[16]--;
		frequency[9]--;
		System.out.println("Custom CMD executed\n");
	}
	
	private void extractDrawNums(String line){
		String delims = "[,]";
		String[] tokens = line.split(delims);
		int drawnum = Integer.parseInt(tokens[0]);
		int temp;
		for(int i = 4; i <= 10 && i< tokens.length;i++){
			temp = Integer.parseInt(tokens[i]);
			frequency[temp - 1]++;
			if(lastdraw[temp-1] == 0){
				lastdraw[temp-1] = drawnum;
			}
		}
	}
	
	public void printNumFreqs(){
		for(int i = 0; i < frequency.length;i++){
			System.out.println("Num: " + (i+1) + " Freq: " + frequency[i]);
		}
	}
	
	public int[][] findLeastCommon(){
		int[] lesspos = new int[6];
		//Least popular
		System.out.println("Entering first for");
		System.out.flush();
		for(int i = lesspos[0]+1;i<frequency.length;i++){
			if(frequency[i] < frequency[lesspos[0]]){
				lesspos[0] = i;
			}
		}
		//System.out.println("Least is num " + (lesspos[0] + 1) + " with freq " + frequency[lesspos[0]]);
		
		//Second least popular
		//Find starting pos - lowest not equal to previous saved pos
		if(lesspos[1] == lesspos[0]){
			lesspos[1]++;
		}
		//Loop through numbers
		for(int i = lesspos[1] + 1;i < frequency.length;i++){
			//If freq of num less than freq of current saved num && != to prev saved
			if(frequency[i] < frequency[lesspos[1]] && i != lesspos[0]){
				lesspos[1] = i;
			}
		}
		//System.out.println("Second least is num " + (lesspos[1] + 1) + " with freq " + frequency[lesspos[1]]);

		//Third least popular
		while(lesspos[2] == lesspos[0] || lesspos[2] == lesspos[1]){
			lesspos[2]++;
		}
		for(int i = lesspos[2] + 1;i < frequency.length;i++){
			if(frequency[i] < frequency[lesspos[2]] && i != lesspos[1] && i != lesspos[0]){
				lesspos[2] = i;
			}
		}
		//System.out.println("Third least is num " + (lesspos[2] + 1) + " with freq " + frequency[lesspos[2]]);

		//Fourth least popular
		while(lesspos[3] == lesspos[0] || lesspos[3] == lesspos[1] || lesspos[3] == lesspos[2]){
			lesspos[3]++;
		}
		for(int i = lesspos[3] + 1;i < frequency.length;i++){
			if(frequency[i] < frequency[lesspos[3]] && i != lesspos[2] && i != lesspos[1] && i != lesspos[0]){
				lesspos[3] = i;
			}
		}
		//System.out.println("Fourth least is num " + (lesspos[3] + 1) + " with freq " + frequency[lesspos[3]]);
		
		//Fifth least popular
		while(lesspos[4] == lesspos[0] || lesspos[4] == lesspos[1] || lesspos[4] == lesspos[2] || lesspos[4] == lesspos[3]){
			lesspos[4]++;
		}
		for(int i = lesspos[4] + 1;i < frequency.length;i++){
			if(frequency[i] < frequency[lesspos[3]] && i != lesspos[3] && i != lesspos[2] && i != lesspos[1] && i != lesspos[0]){
				lesspos[4] = i;
			}
		}
		//System.out.println("Fifth least is num " + (lesspos[4] + 1) + " with freq " + frequency[lesspos[4]]);
		//Sixth least popular
		while(lesspos[5] == lesspos[0] || lesspos[5] == lesspos[1] || lesspos[5] == lesspos[2] || lesspos[5] == lesspos[3] || lesspos[5] == lesspos[4]){
			lesspos[5]++;
		}
		for(int i = lesspos[5] + 1;i < frequency.length;i++){
			if(frequency[i] < frequency[lesspos[3]] && i != lesspos[4] && i != lesspos[3] && i != lesspos[2] && i != lesspos[1] && i != lesspos[0]){
				lesspos[5] = i;
			}
		}
		//System.out.println("Sizth least is num " + (lesspos[5] + 1) + " with freq " + frequency[lesspos[5]]);
		int[][] result = new int[2][];
		result[0] = lesspos;
		result[1] = frequency;
		return result;
	}
}
